package com.example.bbs.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bbs.entity.Comment;
import com.example.bbs.mapper.CommentMapper;
import com.example.bbs.service.CommentService;
import com.example.bbs.util.CommentResolve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @author jiajiazi
 * @version 1.0
 * @date 2021/7/5 3:59
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<CommentResolve> getCommentsOfLevel1(int artid) {
        return commentMapper.getCommentsOfLevel1(artid);
    }

    @Override
    public List<CommentResolve> getCommentsOfLevel2_3(int artid, int comment1id) {
        return commentMapper.getCommentsOfLevel2_3(artid, comment1id);
    }

    @Override
    public String addComment1(int artid, int commentatorid, String content) {
        return addComment(1, artid, commentatorid, -1, -1, content);
    }

    @Override
    public String addComment2(int artid, int commentatorid, int comment1id, String content) {
        return addComment(2, artid, commentatorid, comment1id, -1, content);
    }

    @Override
    public String addComment3(int artid, int commentatorid, int comment1id, int comment2id, String content) {
        return addComment(3, artid, commentatorid, comment1id, comment2id, content);
    }

    @Override
    public String updateSupport(int commentid, int value) {
        String commentkey = "comment_" + commentid;
        redisTemplate.opsForValue().set(commentkey, value);
        System.out.println("redis: commentkey= " + commentkey + "  value: " + value);
        return "200";
    }

    public String addComment(int lev, int artid, int commentatorid, int comment1id, int comment2id, String content) {
        Comment comment = new Comment();
        comment.setArtid(artid);
        comment.setCommentatorid(commentatorid);
        comment.setContent(content);
        comment.setCreatTime(new Date());
        if (lev == 1) {
            comment.setLevel(1);
        } else {
            Integer replynum = commentMapper.selectById(comment1id).getReplynum() + 1;
            Comment c = new Comment();
            c.setId(comment1id);
            c.setReplynum(replynum);
            int i = commentMapper.updateById(c);
            if (i <= 0)
                return "500";
            comment.setComment1id(comment1id);
            if (lev == 2) {
                comment.setLevel(2);
            } else if (lev == 3) {
                comment.setComment2id(comment2id);
                comment.setLevel(3);
            }
        }
        int insert = commentMapper.insert(comment);
        if (insert > 0) {
            return "200";
        }
        return "500";
    }

}
